
import { getOrderSetting } from '@/api/order';
import { initTableList } from './listSettingData';
import { getDynamickey, getViewList } from '@/api/resource';
import { handelListSetting } from '../model/utils';

export default {
  data() {
    return {
      tableListSetting: [],
      allViewData: [],
      allTableList: null,
      curView: ''
    };
  },
  methods: {
    /**
     * 自定义列
     */
    setTableCloumn() {
      this.$refs.tableSettingRef.openDialog(this.allTableList, this.allViewData);
    },
    // 获取自定义列数据
    async getSettingData() {
      const activeTabs = this.activeTabs || 'all';
      await this.getDynamic();
      const curView = this.getLocalView();
      this.curView = '';
      if (curView && curView[activeTabs]) {
        this.changeView(curView[activeTabs]);
      }
      this.getViewList();
      getOrderSetting().then(res => {
        let allData = {};
        if (res.code === 200 && res.data !== '' && res.data.resource) {
          const resource = this.jsonParse(res.data.resource);
          allData = Array.isArray(resource) ? {} : resource;
        }
        const initNormalTableList = this.setNormalTableList();
        allData[activeTabs] = handelListSetting(allData[activeTabs] || initNormalTableList, this.dynamicKeyList, activeTabs);
        this.allTableList = allData;
        if (!curView[activeTabs]) {
          this.tableListSetting = allData[activeTabs];
        }
      }).catch(err => {
        console.log(err);
      });
    },
    // 设置初始筛选展示项
    setNormalTableList() {
      const list = _.cloneDeep(initTableList);
      const currActiveTab = this.activeTabs || 'all';
      if (currActiveTab === 'all') {
        // 签约信息、上刊价格、成本信息、更多上刊信息、更多账号信息默认不勾选
        const emptyArrKeys = ['handoverKey', 'journalKey', 'costInfoKey', 'moreJournalKey', 'accountInfo'];
        list.forEach(item => {
          if (emptyArrKeys.includes(item.fieldName)) {
            item.children && item.children.forEach(i => {
              if (i.fieldName !== 'accountAllField') { // 列表"账号信息"字段默认一直保持显示状态
                i.isShow = false;
              }
            });
          }
        });
      }
      return list;
    },
    resetSeting() {
      this.getSettingData();
      this.getMediumData();
    },
    jsonParse(str) {
      if (typeof str !== 'string') return str;
      try {
        return JSON.parse(str);
      } catch (error) {
        console.log(error);
        return {};
      }
    },
    // 获取动态字段
    getDynamic() {
      return new Promise((resolve) => {
        getDynamickey({ platformType: this.activeTabs }).then((res) => {
          const ans = {};
          if (res.data && res.data.length) {
            res.data.forEach(i => {
              const temp = {};
              if (i.item) {
                i.item.forEach(j => {
                  temp[j.name] = j.item;
                });
              }
              ans[i.platformId.toString()] = temp;
            });
          }
          resolve(ans);
          this.dynamicKeyList = ans;
        }).catch(() => {
          resolve({});
        });
      });
    },
    /**
     * 视图
     */
    changeView(viewData) {
      const activeTabs = this.activeTabs || 'all';
      const loclView = this.getLocalView();
      this.curView = viewData;
      if (viewData && viewData.data) {
        const cloneView = _.cloneDeep(viewData);
        cloneView.data = this.jsonParse(cloneView.data);
        loclView[activeTabs] = cloneView;
        localStorage.setItem('curView', JSON.stringify(loclView));
        const tableListSetting = handelListSetting(cloneView.data || _.cloneDeep(initTableList), this.dynamicKeyList, activeTabs);
        this.tableListSetting = tableListSetting;
      } else {
        delete loclView[activeTabs];
        localStorage.setItem('curView', JSON.stringify(loclView));
        this.tableListSetting = this.allTableList[activeTabs];
      }
    },
    getLocalView() {
      const loclView = localStorage.getItem('curView') || '{}';
      return this.jsonParse(loclView);
    },
    async getViewList(name) {
      try {
        const platform = this.activeTabs || 'all';
        const data = {
          page: 1,
          perPage: 30,
          name: name || '',
          platform
        };
        const res = await getViewList(data);
        if (res.code === 200 && res.data.items) {
          this.allViewData = res.data.items;
        }
      } catch (error) {
        console.log(error);
      }
    }
  }
};
